###########################################################
#
# clutch
#
###########################################################
#
# CLUTCH_VERSION, CLUTCH_SITE and CLUTCH_SOURCE define
# the upstream location of the source code for the package.
# CLUTCH_DIR is the directory which is created when the source
# archive is unpacked.
# CLUTCH_UNZIP is the command used to unzip the source.
# It is usually "zcat" (for .gz) or "bzcat" (for .bz2)
#
# You should change all these variables to suit your package.
# Please make sure that you add a description, and that you
# list all your packages' dependencies, seperated by commas.
# 
# If you list yourself as MAINTAINER, please give a valid email
# address, and indicate your irc nick if it cannot be easily deduced
# from your name or email address.  If you leave MAINTAINER set to
# "NSLU2 Linux" other developers will feel free to edit.
#
# Version 0.4 only works with transmission-daemon 1.22
# Newer versions of clutch are now included in transmission package!
# 
CLUTCH_SITE=http://clutchbt.com/Files
CLUTCH_VERSION=0.4
CLUTCH_SOURCE=Clutch-$(CLUTCH_VERSION).tar.gz
CLUTCH_DIR=Clutch-$(CLUTCH_VERSION)
CLUTCH_UNZIP=zcat
CLUTCH_MAINTAINER=NSLU2 Linux <nslu2-linux@yahoogroups.com>
CLUTCH_DESCRIPTION=WebUI for the Transmission BitTorrent client
CLUTCH_SECTION=web
CLUTCH_PRIORITY=optional
CLUTCH_DEPENDS=transmissiond, php, php-fcgi
CLUTCH_SUGGESTS=lighttpd
CLUTCH_CONFLICTS=transmission

#
# CLUTCH_IPK_VERSION should be incremented when the ipk changes.
#
CLUTCH_IPK_VERSION=4

#
# CLUTCH_CONFFILES should be a list of user-editable files
CLUTCH_CONFFILES=$(TARGET_PREFIX)/etc/clutch.conf $(TARGET_PREFIX)/etc/init.d/S88clutch

#
# CLUTCH_PATCHES should list any patches, in the the order in
# which they should be applied to the source code.
#
#CLUTCH_PATCHES=$(CLUTCH_SOURCE_DIR)/configure.patch

#
# If the compilation of the package requires additional
# compilation or linking flags, then list them here.
#
CLUTCH_CPPFLAGS=
CLUTCH_LDFLAGS=

#
# CLUTCH_BUILD_DIR is the directory in which the build is done.
# CLUTCH_SOURCE_DIR is the directory which holds all the
# patches and ipkg control files.
# CLUTCH_IPK_DIR is the directory in which the ipk is built.
# CLUTCH_IPK is the name of the resulting ipk files.
#
# You should not change any of these variables.
#
CLUTCH_BUILD_DIR=$(BUILD_DIR)/clutch
CLUTCH_SOURCE_DIR=$(SOURCE_DIR)/clutch
CLUTCH_IPK_DIR=$(BUILD_DIR)/clutch-$(CLUTCH_VERSION)-ipk
CLUTCH_IPK=$(BUILD_DIR)/clutch_$(CLUTCH_VERSION)-$(CLUTCH_IPK_VERSION)_$(TARGET_ARCH).ipk

.PHONY: clutch-source clutch-unpack clutch clutch-stage clutch-ipk clutch-clean clutch-dirclean clutch-check

#
# This is the dependency on the source code.  If the source is missing,
# then it will be fetched from the site using wget.
#
$(DL_DIR)/$(CLUTCH_SOURCE):
	$(WGET) -P $(DL_DIR) $(CLUTCH_SITE)/$(@F) || \
	$(WGET) -P $(DL_DIR) $(SOURCES_NLO_SITE)/$(@F)

#
# The source code depends on it existing within the download directory.
# This target will be called by the top level Makefile to download the
# source code's archive (.tar.gz, .bz2, etc.)
#
clutch-source: $(DL_DIR)/$(CLUTCH_SOURCE) $(CLUTCH_PATCHES)

#
# This target unpacks the source code in the build directory.
# If the source archive is not .tar.gz or .tar.bz2, then you will need
# to change the commands here.  Patches to the source code are also
# applied in this target as required.
#
# This target also configures the build within the build directory.
# Flags such as LDFLAGS and CPPFLAGS should be passed into configure
# and NOT $(MAKE) below.  Passing it to configure causes configure to
# correctly BUILD the Makefile with the right paths, where passing it
# to Make causes it to override the default search paths of the compiler.
#
# If the compilation of the package requires other packages to be staged
# first, then do that first (e.g. "$(MAKE) <bar>-stage <baz>-stage").
#
# If the package uses  GNU libtool, you should invoke $(PATCH_LIBTOOL) as
# shown below to make various patches to it.
#
$(CLUTCH_BUILD_DIR)/.configured: $(DL_DIR)/$(CLUTCH_SOURCE) $(CLUTCH_PATCHES) make/clutch.mk
#	$(MAKE) <bar>-stage <baz>-stage
	rm -rf $(BUILD_DIR)/$(CLUTCH_DIR) $(@D)
	$(CLUTCH_UNZIP) $(DL_DIR)/$(CLUTCH_SOURCE) | tar -C $(BUILD_DIR) -xvf -
	if test -n "$(CLUTCH_PATCHES)" ; \
		then cat $(CLUTCH_PATCHES) | \
		$(PATCH) -d $(BUILD_DIR)/$(CLUTCH_DIR) -p0 ; \
	fi
	if test "$(BUILD_DIR)/$(CLUTCH_DIR)" != "$(@D)" ; \
		then mv $(BUILD_DIR)/$(CLUTCH_DIR) $(@D) ; \
	fi
#	(cd $(@D); \
#		$(TARGET_CONFIGURE_OPTS) \
#		CPPFLAGS="$(STAGING_CPPFLAGS) $(CLUTCH_CPPFLAGS)" \
#		LDFLAGS="$(STAGING_LDFLAGS) $(CLUTCH_LDFLAGS)" \
#		./configure \
#		--build=$(GNU_HOST_NAME) \
#		--host=$(GNU_TARGET_NAME) \
#		--target=$(GNU_TARGET_NAME) \
#		--prefix=$(TARGET_PREFIX) \
#		--disable-nls \
#		--disable-static \
#	)
#	$(PATCH_LIBTOOL) $(@D)/libtool
	touch $@

clutch-unpack: $(CLUTCH_BUILD_DIR)/.configured

#
# This builds the actual binary.
#
$(CLUTCH_BUILD_DIR)/.built: $(CLUTCH_BUILD_DIR)/.configured
	rm -f $@
#	$(MAKE) -C $(@D)
	touch $@

#
# This is the build convenience target.
#
clutch: $(CLUTCH_BUILD_DIR)/.built

#
# If you are building a library, then you need to stage it too.
#
$(CLUTCH_BUILD_DIR)/.staged: $(CLUTCH_BUILD_DIR)/.built
	rm -f $@
	$(MAKE) -C $(@D) DESTDIR=$(STAGING_DIR) install
	touch $@

clutch-stage: $(CLUTCH_BUILD_DIR)/.staged

#
# This rule creates a control file for ipkg.  It is no longer
# necessary to create a seperate control file under sources/clutch
#
$(CLUTCH_IPK_DIR)/CONTROL/control:
	@$(INSTALL) -d $(@D)
	@rm -f $@
	@echo "Package: clutch" >>$@
	@echo "Architecture: $(TARGET_ARCH)" >>$@
	@echo "Priority: $(CLUTCH_PRIORITY)" >>$@
	@echo "Section: $(CLUTCH_SECTION)" >>$@
	@echo "Version: $(CLUTCH_VERSION)-$(CLUTCH_IPK_VERSION)" >>$@
	@echo "Maintainer: $(CLUTCH_MAINTAINER)" >>$@
	@echo "Source: $(CLUTCH_SITE)/$(CLUTCH_SOURCE)" >>$@
	@echo "Description: $(CLUTCH_DESCRIPTION)" >>$@
	@echo "Depends: $(CLUTCH_DEPENDS)" >>$@
	@echo "Suggests: $(CLUTCH_SUGGESTS)" >>$@
	@echo "Conflicts: $(CLUTCH_CONFLICTS)" >>$@

#
# This builds the IPK file.
#
# Binaries should be installed into $(CLUTCH_IPK_DIR)$(TARGET_PREFIX)/sbin or $(CLUTCH_IPK_DIR)$(TARGET_PREFIX)/bin
# (use the location in a well-known Linux distro as a guide for choosing sbin or bin).
# Libraries and include files should be installed into $(CLUTCH_IPK_DIR)$(TARGET_PREFIX)/{lib,include}
# Configuration files should be installed in $(CLUTCH_IPK_DIR)$(TARGET_PREFIX)/etc/clutch/...
# Documentation files should be installed in $(CLUTCH_IPK_DIR)$(TARGET_PREFIX)/doc/clutch/...
# Daemon startup scripts should be installed in $(CLUTCH_IPK_DIR)$(TARGET_PREFIX)/etc/init.d/S??clutch
#
# You may need to patch your application to make it use these locations.
#
$(CLUTCH_IPK): $(CLUTCH_BUILD_DIR)/.built
	rm -rf $(CLUTCH_IPK_DIR) $(BUILD_DIR)/clutch_*_$(TARGET_ARCH).ipk
#	$(MAKE) -C $(CLUTCH_BUILD_DIR) DESTDIR=$(CLUTCH_IPK_DIR) install-strip
	$(INSTALL) -d $(CLUTCH_IPK_DIR)$(TARGET_PREFIX)/etc/
	$(INSTALL) -d $(CLUTCH_IPK_DIR)$(TARGET_PREFIX)/share/www/lighttpd/clutch
	cp -rp $(CLUTCH_BUILD_DIR)/* $(CLUTCH_IPK_DIR)$(TARGET_PREFIX)/share/www/lighttpd/clutch/
	$(INSTALL) -m 644 $(CLUTCH_SOURCE_DIR)/clutch.conf $(CLUTCH_IPK_DIR)$(TARGET_PREFIX)/etc/clutch.conf
	$(INSTALL) -d $(CLUTCH_IPK_DIR)$(TARGET_PREFIX)/etc/init.d
	$(INSTALL) -m 755 $(CLUTCH_SOURCE_DIR)/rc.clutch $(CLUTCH_IPK_DIR)$(TARGET_PREFIX)/etc/init.d/S88clutch
#	sed -i -e '/^#!/aOPTWARE_TARGET=${OPTWARE_TARGET}' $(CLUTCH_IPK_DIR)$(TARGET_PREFIX)/etc/init.d/SXXclutch
	$(MAKE) $(CLUTCH_IPK_DIR)/CONTROL/control
	$(INSTALL) -m 755 $(CLUTCH_SOURCE_DIR)/postinst $(CLUTCH_IPK_DIR)/CONTROL/postinst
#	sed -i -e '/^#!/aOPTWARE_TARGET=${OPTWARE_TARGET}' $(CLUTCH_IPK_DIR)/CONTROL/postinst
#	$(INSTALL) -m 755 $(CLUTCH_SOURCE_DIR)/prerm $(CLUTCH_IPK_DIR)/CONTROL/prerm
#	sed -i -e '/^#!/aOPTWARE_TARGET=${OPTWARE_TARGET}' $(CLUTCH_IPK_DIR)/CONTROL/prerm
#	if test -n "$(UPD-ALT_PREFIX)"; then \
		sed -i -e '/^[ 	]*update-alternatives /s|update-alternatives|$(UPD-ALT_PREFIX)/bin/&|' \
			$(CLUTCH_IPK_DIR)/CONTROL/postinst $(CLUTCH_IPK_DIR)/CONTROL/prerm; \
	fi
	echo $(CLUTCH_CONFFILES) | sed -e 's/ /\n/g' > $(CLUTCH_IPK_DIR)/CONTROL/conffiles
	cd $(BUILD_DIR); $(IPKG_BUILD) $(CLUTCH_IPK_DIR)

#
# This is called from the top level makefile to create the IPK file.
#
clutch-ipk: $(CLUTCH_IPK)

#
# This is called from the top level makefile to clean all of the built files.
#
clutch-clean:
	rm -f $(CLUTCH_BUILD_DIR)/.built
	-$(MAKE) -C $(CLUTCH_BUILD_DIR) clean

#
# This is called from the top level makefile to clean all dynamically created
# directories.
#
clutch-dirclean:
	rm -rf $(BUILD_DIR)/$(CLUTCH_DIR) $(CLUTCH_BUILD_DIR) $(CLUTCH_IPK_DIR) $(CLUTCH_IPK)
#
#
# Some sanity check for the package.
#
clutch-check: $(CLUTCH_IPK)
	perl scripts/optware-check-package.pl --target=$(OPTWARE_TARGET) $(CLUTCH_IPK)
